class_name BuffGroupC
extends Node

const component_name := "BuffGroupC"
var _notify: Notify  #set from mutil
var _id: int  #entity id

var action_buff: ActionBuff setget _set_action_buff
var heap: Heap
var round_heap: Heap
var map := {}
var instant := []


func _init():
	heap = Heap.new()
	round_heap = Heap.new()


func add_buff(buff: Buff, fire_type, is_round):
	add_map(buff, fire_type)
	if buff.next_time > 0:
		if is_round:
			round_heap.insert(buff)
		else:
			heap.insert(buff)
	_set_action_buff(ActionBuff.new(Buff.ActionBuffType.add, buff))


func add_interval_buff(buff: Buff):
	heap.insert(buff)
	_set_action_buff(ActionBuff.new(Buff.ActionBuffType.add, buff))


func add_round_buff(buff: Buff):
	round_heap.insert(buff)
	_set_action_buff(ActionBuff.new(Buff.ActionBuffType.add, buff))


func get_heap():
	return heap


func get_round_heap():
	return round_heap


func get_buffs(fire_type: String) -> Set:
	if map:
		return map.get(fire_type)
	return null


func del_buff(buff, fire_type):
	var set: Set = map.get(fire_type)
	if set:
		set.delete(buff)


func get_instant():
	return instant


func add_map(buff, fire_type):
	if not map:
		map = {}
	var set: Set = map.get(fire_type)
	if not set:
		set = Set.new()
		map[fire_type] = set
	set.add(buff)


func _set_action_buff(buff):
	action_buff = buff
	if _notify:
		_notify.modify_event(_id, "action_buff")


class ActionBuff:
	var action
	var buff

	func _init(_action, _buff):
		action = _action
		buff = _buff
